package com.chpro.freemarker;

import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import com.chpro.freemarker.model.EntityAttribute;
import com.chpro.freemarker.util.UpperFirstCharacter;

import freemarker.template.Configuration;
import freemarker.template.SimpleHash;
import freemarker.template.Template;

public class ClassPathPropertiesApplication implements ApplicationContext {

	private static Configuration cfg;
	public static void init(){
		cfg = new Configuration();
		cfg.setClassForTemplateLoading(ClassPathPropertiesApplication.class,"/resources");
		cfg.setSharedVariable("upperFC", new UpperFirstCharacter());//添加一个"宏"共享变量用来将属性名首字母大写  
	}
	
	public boolean buildEntity(String tableName)throws Exception {
		init();
		
		List<EntityAttribute> entityAttributeList = new ArrayList<EntityAttribute>();	
		
		EntityAttribute entityAttribute1 = new EntityAttribute();
		entityAttribute1.setName("id");
		entityAttribute1.setType("String");
		entityAttribute1.setIsPK("true");
		
		EntityAttribute entityAttribute2 = new EntityAttribute();
		entityAttribute2.setName("name");
		entityAttribute2.setType("String");
		entityAttribute2.setIsPK("false");
		
		EntityAttribute entityAttribute3 = new EntityAttribute();
		entityAttribute3.setName("age");
		entityAttribute3.setType("String");
		entityAttribute3.setIsPK("false");
		
		entityAttributeList.add(entityAttribute1);
		entityAttributeList.add(entityAttribute2);
		entityAttributeList.add(entityAttribute3);
		
		SimpleHash root = new SimpleHash();
		root.put("tableName", tableName);
		root.put("entityAttributeList", entityAttributeList);

        
        // Get the templat object
        Template t = cfg.getTemplate("entity.ftl");
        
        Writer out = new FileWriter("c:\\"+tableName+".java");	//输出目录
        t.process(root, out);
		return true;
	}

	public boolean buildDao(String entityName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean buildDaoImpl(String entityName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean buildService(String entityName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean buildServiceImpl(String entityName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean buildController(String entityName) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean buildMVC(String tableName) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

}
